package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * projectName: wokepase1
 *
 * @author: 强力
 * time: 2021/10/8 19:54
 * description:
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private EurekaClient eurekaClient;

    @RequestMapping("/test")
    public String test(){
        //通过eureka获取搜索微服务的ip和端口,根据搜索微服务的服务名去获取
        InstanceInfo info = eurekaClient.getNextServerFromEureka("QF-SEARCH", false);

        //根据eureka返回的对象中获取访问地址url
        String url = info.getHomePageUrl();
        System.out.println("------"+url);
        //通过restTemplate发送http请求访问url地址
        String str = restTemplate.getForObject(url + "/search/test", String.class);
        //返回字符串
        return str;
    }
}
